<script>

var idDisablePedido = 0;
var lastClick = "";

function showOpt(id,obj)
{
	$(".opt_" + id).fadeIn();
	//$(obj).fadeIn();

	hideOpt(lastClick,obj);
}

function closePedido(id)
{
	idDisablePedido = id;
	
	$(".bg_transp").show();

	$.post('pedido/list', {cod: id}, function(res)
	{
		$("#resumList").html(res);
		$(".resumDiv").fadeIn("fast");
	});
}

function listaMesas()
{
	if($(".disable_mesa").css("display") == "block")
	{
		if($(".msgMesas").html())
			$(".msgMesas").show();
		
		$(".disable_mesa").hide();
	}
	else
	{
		$('.msgMesas').hide(); 
		$('.listmesa').fadeIn('fast');
	}
	
}

function hideOpt(id,ob)
{
	$(".opt_" + id).slideUp("fast");

	//$(ob).animate({	width: '90px', height: '70px'}, 100);
}

function openPedido(mesa)
{
	$("#general").hide();

	$.post('pedido/pedido', {cod_mesa: mesa}, function(req)
	{
		$("#editPedido").html(req).fadeIn("fast");
	});
}

function openHistorico(id)
{
	$("#desac_btn").hide();

	$(".bg_transp").show();

	$.post('pedido/list', {mesa: id}, function(res)
	{
		$("#resumList").html(res);
		$(".resumDiv").fadeIn("fast");
	});
}

function disactivePedido()
{
	var forma_pagamento = $("input:radio[name=fp_radio]:checked").val();
	
	$(".menu_cnew").click();
	msg({vmsg: "Mesa <b>" + lastClick + "</b> Fechada.", time: 5000});
	
	$.post('pedido/disactive', {cod_usuario: user.caf01_cod, cod_pedido: idDisablePedido, forma_pagamento: forma_pagamento}, function(req)
	{
		$(".bg_transp").hide();
		$(".resumDiv").fadeOut("fast");
		
		
		printReceive2(idDisablePedido);


		//$("#editPedido").html(req).fadeIn("fast");
	});
}

function printReceive()
{
	window.open("imprimir/imprimirtxt/cod/" + idDisablePedido, "_blank");
}

function printReceive2(id)
{
	window.open("imprimir/imprimirtxt/cod/" + id, "_blank");
}

</script>

<div id="general" style="width: 780px; margin: 0 auto; overflow: auto;">
<div style="margin-bottom: 10px; height: 30px;">
<div style="float: right;">
<div class="button ui-corner-all" onclick="$('#check_mostra_todos').click()" 
style=" background: white; padding: 8px; width: 170px; border: 1px solid #EAEAEA;">
	<div style="float: left; position: relative;">
		<input type="checkbox" id="check_mostra_todos" onclick="listaMesas();" />
	</div>
	<div style="margin-top: 1px; font-size: 15px;">
		&nbsp;Mostrar todas mesas
	</div>
</div>
</div>
<div style="float: right; text-align: right; width: 200px; margin-top: 10px; margin-right: 3px;">
	<label id='load_subproduct' style='display: none;'>&nbsp;<img src='<?php $this->base; ?>/public/images/ajax-loader.gif' /></label>
</div>
<div style="float: left; width: 200px; font-size: 22px; margin-top: 0px;">
	Mesas em Uso
</div>
</div>
<hr />
<div>
	<?php 
	
	error_reporting(0);
	
	$entro = false;
	$arr = array();
	$caf13 = new caf13();
	$arr_caf13=$caf13->find(1)->current()->toArray();
	
	$cod_mesa = "";
	
	foreach ($this->arr_pedido as $key => $val)
	{
		$arr[] = $val['caf02_mesa'];
		$user[$val['caf02_mesa']] = $val['caf01_user'];
		$user_cod[$val['caf02_mesa']] = $val['caf01_cod'];
		$cod[$val['caf02_mesa']] = $val['caf02_cod'];
		
		if($val["caf02_mesa"] == $cod_mesa)
		{
			$valor[$val['caf02_mesa']] += $val['caf05_valor'];
			$desconto[$val["caf02_mesa"]] = $val['caf02_desconto'];
		}
		else
		{
			$valor[$val['caf02_mesa']] = $val['caf05_valor'];
			$desconto[$val["caf02_mesa"]] = $val['caf02_desconto'];
		}
		
		$cod_mesa = $val["caf02_mesa"];
	}
	
	for($i = $arr_caf13["caf13_mesa_incial"]; $i < $arr_caf13["caf13_mesa_final"]; $i++)
	{
		$used = "";
		$bg = "";
		
		if(array_search($i, $arr) === false)
		{
		?>
		<div class="listmesa disable_mesa" style="float: left; display: none;">
<div
	onclick="showOpt(<?php echo $i; ?>, this); lastClick = '<?php echo $i; ?>';"
	class="button ui-corner-all" type="button"
	style="font-size: 12px; padding-top: 10px; opacity: 0.7; text-align: center; 
	width: 90px; height: 85px; margin: 5px; border: 1px solid gray; border-top: 0; border-right: 0;">
				<b>Mesa <?php echo $i; ?></b>
			</div>
<div
	style="opacity: 0.9; position: absolute; z-index: 1; display: none; text-align: left; font-size: 24px; width: 166px; border: 1px solid #B0ADAD; background-color: white; margin-left: 3px;"
	class="ui-widget ui-corner-all unit opt_<?php echo $i; ?>">
<div onclick="openHistorico('<?php echo $i; ?>');"
	style="border-bottom: 1px solid #B0ADAD; padding: 5px;">Ultima Ordem</div>
</div>
</div>
		<?
		}
		else 
		{
			$entro = true;
		?>
		<div class="listmesa" style="float: left; <?php echo $bg; ?>">
<div
	onclick="showOpt(<?php echo $i; ?>,this); lastClick = '<?php echo $i; ?>';"
	class="button ui-corner-all" type="button"
	style="font-size: 12px; padding-top: 10px; text-align: center; width: 90px; 
	height: 85px; margin: 5px; border: 1px solid gray; border-top: 0; border-right: 0;">
<font style="">
				<?php
				if($this->userId == $user_cod[$i] || $this->gerente)
				{
					echo "<b>{$user[$i]}</b>";
				}else{
					echo "$user[$i]";
				} 
				?>
				</font> <br />
<strong>Mesa <?php echo $i; ?></strong> <br />
<font color="red">R$<?php echo Num::money($valor[$i] - $desconto[$i]); ?></font>
<div>
	cod.<?php echo $cod[$i]; ?>
</div>
</div>
<div
	style="opacity: 0.9; position: absolute; z-index: 1; display: none; text-align: left; 
	font-size: 24px; width: 204px; border: 1px solid #B0ADAD; background-color: white; 
	margin-left: 4px; margin-top: -4px;"
	class="ui-widget ui-corner-all unit opt_<?php echo $i; ?>">
<div onclick="edpedido = 1; openPedido('<?php echo $cod[$i]; ?>');"
	style="border-bottom: 1px solid #B0ADAD; padding: 5px;">Ordem</div>
<div onclick="printReceive2('<?php echo $cod[$i]; ?>');"
	style="border-bottom: 1px solid #B0ADAD; padding: 5px;">Imprimir</div>
<div onclick="closePedido('<?php echo $cod[$i]; ?>');"
	style="border-bottom: 1px solid #B0ADAD; padding: 5px;">Fechar Mesa</div>
</div>
</div>
	<?
		}
	}
	
	if(!$entro)
		echo $this->partial("helpers/msgvazio.phtml", array("msg" => "Nenhuma mesa em uso."));
	?>
	
	<div style="margin-top: 10px;"></div>
<!-- 
	<a href="#" onclick="$('#allpedidos').fadeIn('fast');" style="color: blue;">[+]Ver todos pedidos</a>
	<hr />
	<div id="allpedidos" style="display: none;">
		<table id="tbl_list" width="100%" style="font-size: 12px;">
			<?php 
			
			$total_geral = 0;
			$mesa        = 0;
			
			foreach ($this->arr_pedido as $key => $val)
			{
				$total_prod  = $val["caf05_valor"] * $val["caf04_qtd"];
				$total_geral += $total_prod;
				
				if($mesa != $val['caf02_mesa'] && $mesa)
				{
					echo "<tr><td colspan='5'>Mesa: {$val['caf02_mesa']}</td></tr>";
				}
				
				if($mesa == 0)
				{
					echo "<tr><td colspan='5'>Mesa: {$val['caf02_mesa']}</td></tr>";
				}
			?>
				<tr>
					<td width="1%">
						<?php echo $val["caf04_qtd"]; ?>
					</td>
					<td>
						<?php echo "{$val["caf05_prod"]}-{$val["caf05_car"]}"; ?>				
					</td>
					<td width="1%">
						$<?php echo $val["caf05_valor"]; ?>/un.&nbsp;&nbsp;
					</td>
					<td width="1%">
						$<?php echo $total_prod; ?>	
					</td>
					<td width="1%">
						<input type="button" value="Excluir" />
					</td>
				</tr>
			<?
				$mesa = $val['caf02_mesa'];
			}
			
			?>
		</table>
	</div>
	--></div>
</div>
<div style="display: none;" id="editPedido"></div>